<?php
include ('printBoletaNew.php');
exit;
header('Content-Type: text/html; charset=iso-8859-1');
$wl = 250;
include ('../Connections/KRAPPERCON.php');
require('../Libs/fpdf/fpdf.php');
require('../Libs/fpdf/extend.php');
	$tipo = '';
	$dirLocal = '';
	$obsLocal = '';
	$placa = '';
	$FacturaID = '';
	$cliente = '';
	$vendedor = '';
	$fecha ='';
	$kardex = 0;
	$fact_neto = 0;
	$fact_igv = 0;
	$fact_tot = 0;
	$fecven = '';
	$ordenID = '';
	$cladoc = '';
	$condpag = '';
	$newfec2 = '';
	$cldoc = '';
	$hora = '';
	$hora  = '';
	$direccion = '';
	$ruc = '';
	$fol = '';
	$showDct = 0;
	$usrid = '';
        
	$oper='';
	$factura='';	
		
if(isset($_GET['oper'])){
	$oper=$_GET['oper'];
}else{
    $oper='print';	
}

//$oper = $_GET['oper'];
//$oper = 'print';

if ($oper == 'print'){


 if( isset($_GET['factura']) ){
	 $factura=$_GET['factura'];
 }else{
	 $factura='9000009';
 }

//$factura = $_GET['factura'];
//$factura  = '200000074';

$query="call sp_printfactura ('".$factura."');";
//echo $query;

$res = $mysqli->query($query);
if ($res) {
	if ($row = $res->fetch_assoc() ){
	$FacturaID = $row['factura'];	
	$cliente = $row['nomcli'];
	$vendedor = $row['usrnom'];
	$condpag = $row['condvta'];
	$usrid = $row['user'];
	$fact_neto = $row['w_factNet'];
	$fact_igv = $row['w_factIva'];	
	$fact_tot = $fact_neto + $fact_igv;
	$ordenID = $row['ordenID'];
	$fecven = $row['fecven'];
    //$showDct = $row['showDct'];
	//$fecfac = $row['FacFecFac'];
	$cladoc = substr($row['cladoc'],3,2);
	$kardex  = $row['kardex'];
	if ($cladoc == 'BL'){
		$tipo = 'BOLETA';
	}
	elseif ($cladoc == 'FC'){
		$tipo = 'FACTURA';	
	}
	elseif ($cladoc == 'NF' || $cladoc == 'NB' ){
		$tipo = 'NOT.CREDITO';	
	}
	elseif ($cladoc == 'BA' || $cladoc == 'FA' ){
		$tipo = 'BOL.ANTICIPO';	
	}
	
	
	$fecha = $row['fecfac'];
	// 2012-12-12
	$newfec = substr($fecha,8,2)."/".substr($fecha,5,2)."/".substr($fecha,0,4);
	$hora = $row['hora'];
	//echo $hora;
	//$hora  = '';
	$texto = $row['texto'];
	$direccion = $row['dircli'];
	$ruc = $row['nifcli'];
	$folio = $row['folio'];	$dirLocal = $row['dirLocal'];
	$obsLocal = $row['obsLocal'];

	}
	$res->close();
	$mysqli->check_result();
}
$typ = 'D';
$sx = 5	;
$fnt = 'Arial';
//$pdf=new PDF('P','mm',array(225,222));
$pdf=new PDF('P','mm','A4');
$pdf->AddPage();
$pdf->AddFont('Dot','','dot.php');
$pdf->SetMargins(0,0,13);
$pdf->SetFont($fnt,'',18);
$pdf->SetAutoPageBreak(true, 0); 

$pdf->Ln(33);

$pdf->SetFont($fnt,'',10);
$pdf->SetX($sx + 110);
$pdf->Cell(115,7,$FacturaID . " / " . $folio,'',1,'C');

$pdf->Cell(1,7,'','',1,'');
$pdf->Cell(1,7,'','',1,'');

$pdf->SetX($sx);
$pdf->Cell(115,7,str_repeat(' ', 18). $cliente, '', 0, 'L'); // Raz.Social de Cliente
$pdf->SetX($sx+155);
$pdf->Cell(10,7,$newfec, '', 1, 'L'); // RUC/DNI de Cliente

$pdf->SetX($sx);
$pdf->Cell(115,7,str_repeat(' ', 18).$direccion, '', 0, 'L'); // RUC/DNI de Cliente
$pdf->SetX($sx+155);
$pdf->Cell(115,7,$ruc, '', 1, 'L'); // Fecha de Emisión

$pdf->Cell(1,7,'','',1,'');

$pdf->SetX($sx);
$pdf->SetX($sx);
/*
                //detalle
			    $pdf->Cell(10,5, "ALM.", 0, 0, 'C');
				$pdf->Cell(95,5, "ARTICULO", 0, 0, 'C');
				$pdf->Cell(22,5, "CANTIDAD", 0, 0, 'R');
				$pdf->Cell(22,5, "DSCTO".($showDct==1?"(%)":""), 0, 0, 'R');
				//$pdf->Cell(25,5, "I.G.V", 0, 0, 'C');
				if ($kardex==1){
				$pdf->Cell(22,5, "PR.UNIT.S/.", 0, 0, 'R');
				}
				else {
				$pdf->Cell(22,5, "           	", 0, 0, 'R');
				}
				$pdf->Cell(22,5, "IMPORTE S/.", 0, 1, 'R');
				*/
		
$pdf->SetX($sx);
//pdf->Cell($wl, 0, "", 1, 2, 'L', true);
//$pdf->doLine();
//$pdf->Cell(197,0,'','T',true);
$pdf->SetX($sx);			
//$pdf->Ln(1);			

$neto1 = 0;
$neto2 = 0;
$neto = 0;
$pru = 0;
$dscto = 0 ;
$query = "call sp_getFacturaDet('".$factura."')";
$res4 = $mysqli->exeQuery($query);
$igv = 0;
$igv_por = 18;

$print_dct = 0;
$lines = 9;
$row_count = 0;

if ($res4) {
while ($row3 = $res4->fetch_assoc() )			
			{
				$row_count++;
			    $pdf->SetX($sx);
				$pdf->SetFont('Arial','',10);
			    $pdf->Cell(32,5, $row3['DetFacQty'].'  '.$row3['DetFactUM'], 0, 0, 'R');
				$pdf->Cell(81,5,strtoupper($row3['ArticuloID']. str_repeat(' ',20-strlen($row3['ArticuloID'])). utf8_decode($row3['DetFactTxt'])),0, 0, 'L');
               
				if ($kardex == 1) {
				$pdf->Cell(45,5,fmt( abs($row3['DetFactPru'])), 0, 0, 'R');
				}
				else {
				$pdf->Cell(45,5,"", 0, 0, 'C');
				}
				
				$pdf->Cell(26,5, fmt(abs($row3['DetFactNet'])), 0, 1, 'R');
					
			}
			//$row_count=$mysqli->num_rows;
			for($i=0;$i<($lines-$row_count);$i++){
			$pdf->Cell(10,5,'', 0, 1, '');
			}
			
			$res4->close();
			$mysqli->check_result();
			
			}
		
		$pdf->Cell(10,7,'', 0, 1, '');
		$query2="call sp_getNroletras ($fact_tot,'Nuevos Soles');";
		$monto_letras = '';
		
		$res2 = $mysqli->query($query2) or die($mysqli->error.__LINE__);
		if ($res2) {
			if($row2 = $res2->fetch_assoc()) {
			$monto_letras = $row2['res'];
			}
			$res2->close();
		$mysqli->check_result();
		}
			
		$pdf->SetFont($fnt,'B',10);
		$pdf->SetX($sx);
		$pdf->Cell($sx,5,$monto_letras, 0, 0, 'L');
		
		$pdf->SetX($sx+140);
		$pdf->Cell($sx,5,'IGV S/.', 0, 0, 'L');
		
		$pdf->SetX($sx+176);
		$pdf->Cell(10,5,myRound($fact_igv), 0, 1, 'R');		
	
	$pdf->SetFont($fnt,'B',11);
	
	//$texto = 'POR INSTRUCCION DEL VENDEDOR';
	/* Texto de Pie */
	if (trim($texto)!='') {
	$texto = 'NOTA: '.$texto;
	$pdf->SetX($sx+45);
	$pdf->Cell(125,7, $texto,0,0,'L');			
	}
	
	$pdf->SetX($sx+176);
		$pdf->Cell(10,7,myRound($fact_tot), 0, 1, 'R');
		


$pdf->Output('Boleta_'.$FacturaID.'.pdf','D');

}


?>